package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.Constants;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class LoansAttestationServiceImpl implements LoansAttestationService{
    protected static final Logger LOG = LoggerFactory.getLogger(LoansAttestationServiceImpl.class);
    // 将所有的modelMapper注入
    @Autowired
    private LoansAttestationMapper loansAttestationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LimitRatioMapper limitRatioMapper;
    @Autowired
    private MyTeamMapper myTeamMapper;
    @Autowired
    private  ConstantMapper constantMapper;


    @Override
    public LoansAttestation create(LoansAttestation item) {
         loansAttestationMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return loansAttestationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(LoansAttestation item) {
        return loansAttestationMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public LoansAttestation get(Integer id) {
        return loansAttestationMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<LoansAttestation> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,LoansAttestation item){
       LoansAttestationExample  example = new LoansAttestationExample();
       LoansAttestationExample.Criteria criteria = example.createCriteria();
       if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAuthPrice() != null ){
            criteria.andAuthPriceEqualTo(item.getAuthPrice());
        }
        if(item.getLimits() != null ){
            criteria.andLimitsEqualTo(item.getLimits());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreditStatus() != null ){
            criteria.andCreditStatusEqualTo(item.getCreditStatus());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<LoansAttestation> list = this.loansAttestationMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<LoansAttestation> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,LoansAttestation item){
       LoansAttestationExample  example = new LoansAttestationExample();
       LoansAttestationExample.Criteria criteria = example.createCriteria();
       if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAuthPrice() != null ){
            criteria.andAuthPriceEqualTo(item.getAuthPrice());
        }
        if(item.getLimits() != null ){
            criteria.andLimitsEqualTo(item.getLimits());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreditStatus() != null ){
            criteria.andCreditStatusEqualTo(item.getCreditStatus());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<LoansAttestation> list = this.loansAttestationMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,LoansAttestation item){
        LoansAttestationExample  example = new LoansAttestationExample();
        LoansAttestationExample.Criteria criteria = example.createCriteria();
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAuthPrice() != null ){
            criteria.andAuthPriceEqualTo(item.getAuthPrice());
        }
        if(item.getLimits() != null ){
            criteria.andLimitsEqualTo(item.getLimits());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreditStatus() != null ){
            criteria.andCreditStatusEqualTo(item.getCreditStatus());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<LoansAttestation> list = this.loansAttestationMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(loansAttestation -> JSONObject.parseObject(JSONObject.toJSONString(loansAttestation))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }

    @Override
    public int updateLpans(Integer userId,Double amount,Date date,Integer user) {

        Integer level = 0;
        loans(userId,level,date,amount,user);

        return 0;
    }

    @Override
    public LoansAttestation getByUser(Integer userId) {
        LoansAttestation loansAttestation = loansAttestationMapper.selectByUserId(userId);
        return loansAttestation;
    }

    public void loans(Integer userId,Integer level,Date date,Double amount,Integer users){
        User user = userMapper.selectByPrimaryKey(userId);
        if (user!=null){
            User user1 = userMapper.selectByPrimaryKey(user.getInviter());
            if (user1!=null){
                level+=1;
                Integer id = user1.getId();
                LoansAttestation loansAttestation = loansAttestationMapper.selectByUserId(id);
                if (loansAttestation!=null){
                    double v  = 0.0;
                    if (level<=20){
                        LimitRatio limitRatio = limitRatioMapper.selectByLevel(level);
                        v = amount * limitRatio.getRatio();

                        if (level==1){
                            Constant constant = constantMapper.selectByKey(Constants.LOANS_RATIO);
                            Double aDouble = Double.valueOf(constant.getItemValue());
                            double v1 = amount * aDouble;
                            v+=v1;
                        }
                    }

                    if (level>20){
                        v=0;
                    }
                    MyTeam byUserId = myTeamMapper.getByUserId(id, users);
                    int i = 0;
                    if (byUserId==null){
                        MyTeam myTeam = new MyTeam();
                        myTeam.setInvestId(id);
                        myTeam.setLevel(level);
                        myTeam.setUserId(users);
                        myTeam.setLimits(v);
                        myTeam.setCreateTime(date);
                        myTeam.setLsatUpdateTime(date);
                         i = myTeamMapper.insert(myTeam);
                    }else{
                        byUserId.setLimits(v);
                         i = myTeamMapper.updateByPrimaryKey(byUserId);
                    }

                    if (i!=0) {
                        loansAttestation.setLimits(loansAttestation.getLimits() + v);
                        loansAttestation.setLastUpdateTime(date);
                        loansAttestationMapper.updateByPrimaryKey(loansAttestation);
                    }
                }else{
                    MyTeam myTeam = new MyTeam();
                    myTeam.setInvestId(id);
                    myTeam.setLevel(level);
                    myTeam.setUserId(users);
                    myTeam.setLimits(0.0);
                    myTeam.setCreateTime(date);
                    myTeam.setLsatUpdateTime(date);
                    int insert = myTeamMapper.insert(myTeam);
                }
                loans(id,level,date,amount,users);
            }
        }
    }

}
